package com.lzx.demo.work6;

import java.util.ArrayList;

/**
 * @author 程序员星星
 * @date 2023/5/4
 * @Description
 */
public class Bank {
    public String BankName;
    private ArrayList<Account> account;

    Bank(String bankName) {
        this.BankName = bankName;
        account = new ArrayList<Account>();
    }

    public Bank(String BankName, ArrayList<Account> account) {
        this.BankName = BankName;
        this.account = account;
    }

    //开账户
    public void OpenAccount(String user, String passcode, double money) {
        account.add(new Account(user, passcode, money));
    }

    //查找账户按用户名,若找到，返回该账户，否则，返回null
    public Account SeekAccount(String username) {
        for (Account account : account) {
            if (username.equals(account.getUsername()))
                return account;
        }
        return null;
    }

    //显示该用户账户信息
    public void showAccount(String userName) {
        Account account = SeekAccount(userName);
        if (account == null)
            System.out.println("该账户不存在。");
        else
            account.show();
    }

    //向特定账户存钱
    public void saveMoney(String userName, double money) {
        Account account = SeekAccount(userName); //查找
        if (account == null)
            System.out.println("该账户不存在。");
        else
            account.saveMoney(money);
    }

    //从特定账户取钱,需验证密码
    public void drawMoney(String userName, double money, String password) {
        Account account = SeekAccount(userName); //查找
        if (account == null)
            System.out.println("该账户不存在。");
        else {
            if (password.equals(account.getPasscode())) {
                account.drawMoney(money);
            } else {
                System.out.println("密码不正确，取钱失败。");
            }
        }
    }
}
